//
//  EntrustTableViewCell.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/27.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol EntrustCellDelegate: class {
    
    /// 撤单
    ///
    /// - Parameter model: model
    func changeStates(_ model:CoinOrderModel)
}
class EntrustTableViewCell: BaseTableViewCell {
    var model:CoinOrderModel = CoinOrderModel()
    weak var delegate:EntrustCellDelegate?
    var isCurrent:Bool = false //判断是否当前委托
    lazy var buyLabel: UILabel = {
        let label = UILabel()
        label.textColor = kThemeColor
        label.font = UIFont(name: "PingFang-SC-Heavy", size: 16)
        return label
    }()
    
    lazy var coinKindLabel: UILabel = {
        let label = BaseLabel(style: .oneStyle)
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    lazy var stateBtn: CustomBtn = {
        let btn = CustomBtn(style: .styleLeft)
        btn.addTarget(self, action: #selector(stateBtnAction), for: UIControlEvents.touchUpInside)
        btn.nameLabel.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
    var timeLabel:UILabel?
    var priceLabel:UILabel?
    var entrustLabel:UILabel?
    var amountLabel:UILabel?
    var averageLabel:UILabel?
    var dealLabel:UILabel?
    
    var timeNameLabel:UILabel?
    var priceNameLabel:UILabel?
    var entrustNameLabel:UILabel?
    var amountNameLabel:UILabel?
    var averageNameLabel:UILabel?
    var dealNameLabel:UILabel?
    
    var type:OrderSelectType?
    
    
    override func setUpUI() {
        super.setUpUI()
        self.contentView.addSubview(buyLabel)
        buyLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(20)
        }
        
        self.contentView.addSubview(coinKindLabel)
        coinKindLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(56)
            make.centerY.equalTo(buyLabel)
        }
        
        self.contentView.addSubview(stateBtn)
        stateBtn.nameLabel.textColor = kThemeColor
        stateBtn.photoImageView.image = UIImage(named: "entrustArrow")
        stateBtn.snp.makeConstraints { (make) in
            make.right.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(buyLabel)
            make.width.equalTo(60)
            make.height.equalTo(40)
        }
        stateBtn.photoImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        stateBtn.nameLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
        
        for i in 0..<6 {
            let titleLabel = BaseLabel(style: .thirdStyle)
            titleLabel.font = UIFont.systemFont(ofSize: 12)
            titleLabel.text = KCoinEntrustArr[i]
            titleLabel.tag = nameTag + i
            self.contentView.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(i<3 ? 50:100)
                make.left.equalToSuperview().offset(15+CGFloat(Int(i%3))*(kSCREEN_WIDTH-30)/3)
                make.width.equalTo((kSCREEN_WIDTH-30)/3)
                make.height.equalTo(12)
            }
            
            let numberLabel = BaseLabel(style: .twiceStyle)
            numberLabel.font = UIFont.systemFont(ofSize: 14)
            numberLabel.tag = inforTag + i
            self.contentView.addSubview(numberLabel)
            numberLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(titleLabel)
                make.width.equalTo((kSCREEN_WIDTH-30)/3)
                make.top.equalTo(titleLabel.snp.bottom).offset(10)
                make.height.equalTo(14)
            }
            if i == 0 {
                timeNameLabel = titleLabel
                timeLabel = numberLabel
            }else if i == 1 {
                priceNameLabel = titleLabel
                priceLabel = numberLabel
            }else if  i == 2 {
                entrustNameLabel = titleLabel
                entrustLabel = numberLabel
            }else  if i == 3 {
                amountNameLabel = titleLabel
                amountLabel = numberLabel
            }else if i == 4 {
                averageNameLabel = titleLabel
                averageLabel = numberLabel
            }else {
                dealNameLabel = titleLabel
                dealLabel = numberLabel
            }
            
            if i%3 ==  0  {
                titleLabel.textAlignment = NSTextAlignment.left
                numberLabel.textAlignment = NSTextAlignment.left
            }else if i%3 == 1{
                titleLabel.textAlignment = NSTextAlignment.center
                numberLabel.textAlignment = NSTextAlignment.center
            }else {
                titleLabel.textAlignment = NSTextAlignment.right
                numberLabel.textAlignment = NSTextAlignment.right
            }
        }
        
    }
    
    func exchangeOderType(type:OrderSelectType) {
        self.type = type
        stateBtn.isHidden = true
        if type == .orderEntrust {
            let text1 = kText("委托价") + "(BTC)"
            let text2 = kText("数量") + "(ETH)"
            let text3 = kText("实际成交") + "USDT"
            let arr = [text1,text2,text3]//["委托价(BTC)","数量(ETH)","实际成交USDT"]
            for i in 0 ..< 6 {
                let nameLabel = self.contentView.viewWithTag(i+nameTag) as? UILabel
                let inforLabel = self.contentView.viewWithTag(i+inforTag) as? UILabel
                if i < 3 {
                    nameLabel?.text = arr[i]
                    nameLabel?.isHidden = false
                    inforLabel?.isHidden = false
                }else {
                    nameLabel?.isHidden = true
                    inforLabel?.isHidden = true
                }
            }

        }
        
    }
     func setModel(model: CoinOrderModel) {
        self.model = model 
        if self.type == .orderEntrust {
            timeLabel?.text = model.tradeType == 0 ? kText("市价")  : CYCalculate.cy_intercept(number: self.model.tradePrice, digit: 6)
            priceLabel?.text = CYCalculate.cy_intercept(number: self.model.tradeAmount, digit: 6)
            entrustLabel?.text = CYCalculate.cy_intercept(number: self.model.dealAmount, digit: 6)
            
            timeNameLabel?.text = String(format: "%@(%@)", kText("价格"),model.sourceCoin)
            priceNameLabel?.text = String(format: "%@(%@)",kText("数量"),model.targetCoin)
            entrustNameLabel?.text = String(format: "%@(%@)",kText("实际成交"),model.targetCoin)
            if model.tradeType == 0 {
                if model.position == 0 {
                    priceNameLabel?.text = String(format: "%@(%@)",kText("委托额"),model.sourceCoin)
                    entrustNameLabel?.text = String(format: "%@(%@)",kText("成交总额"),model.sourceCoin)
                }else {
                    priceNameLabel?.text = String(format: "%@(%@)",kText("委托额"),model.targetCoin)
                    entrustNameLabel?.text = String(format: "%@(%@)",kText("成交总额"),model.targetCoin)
                }
            }
            
        }else {
            timeLabel?.text = model.realTime
            priceLabel?.text =  model.tradeType == 0 ? kText("市价") : CYCalculate.cy_intercept(number: self.model.tradePrice, digit: 6)
            entrustLabel?.text = CYCalculate.cy_intercept(number: self.model.tradeAmount, digit: 6)
            amountLabel?.text = model.allPrice.count != 0 ? model.allPrice : "0"
            averageLabel?.text = CYCalculate.cy_intercept(number: self.model.dealPrice, digit: 6)
            dealLabel?.text = self.model.haveDeal
            timeNameLabel?.text = String(format: "%@",kText("时间"))
            priceNameLabel?.text = String(format: "%@(%@)",kText("委托价"),model.sourceCoin)
            var tradeTitle:String = kText("委托量")
            var tradeCoinName:String = ""
            if (model.tradeType == 0 && model.position == 0) {
                tradeTitle = kText("委托额")
                tradeCoinName = model.sourceCoin
            }else {
                tradeCoinName = model.targetCoin
            }
            entrustNameLabel?.text = String(format: "%@(%@)",tradeTitle,tradeCoinName)
            
            amountNameLabel?.text = String(format: "%@(%@)",kText("成交总额"),(model.tradeType == 0 && model.position != 0) ?  model.targetCoin : model.sourceCoin)
            averageNameLabel?.text = String(format: "%@(%@)",kText("成交均价"),model.sourceCoin)
            dealNameLabel?.text = String(format: "%@(%@)",kText("已成交"),model.targetCoin)
        }
        
        
        let position = self.model.position == 0 ? kText("买入") : kText("卖出")
        let coinName = String(format: "%@/%@", model.targetCoin,model.sourceCoin)
        buyLabel.text = position
        buyLabel.textColor = position == kText("买入") ? kGreenColor : kRedColor
        coinKindLabel.text = coinName
        
        stateBtn.isHidden = false
        stateBtn.photoImageView.isHidden = false
        stateBtn.nameLabel.textColor = kThemeColor
        stateBtn.nameLabel.snp.updateConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
        if model.status == 0 || model.status == 1 || model.status == 3 || model.status == 5{
            stateBtn.nameLabel.text = kText("撤销")
            stateBtn.photoImageView.isHidden = true
            stateBtn.nameLabel.snp.updateConstraints { (make) in
                make.right.equalToSuperview().offset(0)
            }
        }else if model.status == 2 {
            stateBtn.nameLabel.text = kText("完全成交")
        }else if model.status == 4 {
            stateBtn.nameLabel.text = kText("部分成交已撤")
        }else if model.status == 6 {
            stateBtn.nameLabel.text = kText("已撤销")
            stateBtn.photoImageView.isHidden = true
            stateBtn.nameLabel.textColor = KUseTitleColor
            stateBtn.nameLabel.snp.updateConstraints { (make) in
                make.right.equalToSuperview().offset(0)
                make.centerY.equalToSuperview()
            }
        }
    }

    @objc func stateBtnAction() {
        if delegate != nil && delegate?.changeStates != nil {
            self.delegate?.changeStates(self.model)
        }
    
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
